#include "main.h"

//船型开关

//全局变量
uint8_t last_dev_state=0;//前一个状态
uint8_t dev_state=0;//设备状态，0为关机模式，1为开机模式

//按键标志

uint8_t key_sign=0;
uint8_t key_val=1;
uint32_t key_sign_time=0;
uint8_t stop_stop=0;
uint8_t key_direction=0;

uint32_t adcdwncnt2=0;

//停机操作
uint8_t stop_sign=1;//停机标志
uint8_t stop_sign_t=0;
uint32_t delay_time_stop=0;//延时断电时间 ,单位15秒
#define stop_time 15*1000

uint8_t led_sign = 1;
uint32_t led_time = 1000;


//主函数
int main()
{
	uint32_t voltage=0;
	
	SysTick_Config_Init();
	USARTConfig();
	
	printf("Program start!\n\r");
	printf("\n\r");
	printf("\n\r");
	printf("\n\r");
	key_init();

	//上点检测一下按键的状态
	if (Key == 0)
	{
		dev_state=1;
		stop_sign=0;
	}
	
	while(1)
	{
		//关机进程
		if(stop_sign == 1 && stop_sign_t == 0){
			//关机前的操作
			opi_off();//通知板卡执行关机
			led_off();//关灯
			stop_sign_t=1;
			printf(">>> ready stop ing.\n\r");
		}
		//关机
		if(stop_sign == 1 && delay_time_stop == 0){
			printf(">>> cut power!\n\r");
			relay_off();//断开12V

			stop_sign=0;
			if(stop_stop == 0){
				//立刻停机
				printf(">>> stop!\n\r");
				dev_state=0;
				stop_run();//停机
			}else{
				//重新开机
				printf("auto Restart!");
				dev_state=1;
				//printf(">>> key power on !\n\r");	
				SYSCLKConfig_STOP();SysTick_Config_Init();
		
				USARTConfig();
				key_init();adc_init();
				relay_init();led_init();opi_init();
				stop_sign_t=0;stop_stop=0;
				printf(">>> start!\n\r");
			}
		}

		//按键有效性判断
		if (key_sign == 1)
		{
			
			key_val=Key;
			//Delay_ms(20);//
			//if (key_val == Key)
			{
				//检测是否稳定
				int i=60;
				while (i--)
				{
					Delay_ms(50);
					if (Key != key_val)
					{
						//乱按
						key_val=2;
						break;
					}
				}
				key_sign=0;	//本次按键事件处理结束

				switch (key_val)
				{
				case 0://按下
					//关机过程中，关机
					if (dev_state==0) //从停机状态唤醒
					{	
						dev_state=1;
						printf(">>> key power on !\n\r");	
						SYSCLKConfig_STOP();SysTick_Config_Init();
				
						USARTConfig();
						key_init();adc_init();
						relay_init();led_init();opi_init();
						stop_sign=0;stop_sign_t=0;stop_stop=0;
						printf(">>> start!\n\r");
					}else{
						//stop_stop=1;
						if(stop_sign == 1)
						{
							stop_stop = 1; //从停机到开机
						}
					}
					break;
				case 1://松开
					if(dev_state == 0)
					{
						//hui dao ting ji
						printf(">>> stop to stop.\n\r");
						stop_run();//停机					
					}
					else
					{
						//	
						stop_sign=1; //kai shi dao ji shi biao zhi
						delay_time_stop=stop_time;//赋值开始计算
					}
					printf(">>> key power off !\n\r");
					break;
				default:
					if (dev_state == 0)
					{
						printf(">>> XXX\n\r");
						stop_run();//停机
					}	
					break;
				}
			}
		}else{

		}

/*
		//开关机动作
		if (dev_state != last_dev_state)
		{
			switch (dev_state)
			{
			case 0:
				//进入关机进程
				stop_sign=1;
				delay_time_stop=stop_time;//赋值开始计算
				break;
			case 1:
				//初始化
				SYSCLKConfig_STOP();SysTick_Config_Init();
				
				USARTConfig();
				key_init();adc_init();
				relay_init();led_init();opi_init();
				stop_sign=0;stop_sign_t=0;stop_stop=0;
				printf(">>> start!\n\r");
				break;
			default:
				break;
			}
			last_dev_state=dev_state;
		}
*/
		//运行状态的逻辑
		if (dev_state == 1 )
		{
			//Delay_ms(1000);
			if(adcdwncnt2 == 0)
			{
				adcdwncnt2 = 1000;
				voltage=ADC_Check();
				printf(">>> voltage:%0.3fV \n\r",(float)voltage/1000);
			// printf(">>> dev_state:%d last_dev_state:%d \n\r",dev_state,last_dev_state);
			}

		}
		
	}
}
//按键触发
void EXTI0_1_IRQHandler(void)
{
    if(EXTI_GetITStatus(EXTI_Line0)!=RESET) //确保中断是否产生
    {	
		EXTI_ClearFlag(EXTI_Line0); //清除中断标志位
		SYSCLKConfig_STOP();SysTick_Config_Init();
		USARTConfig();
		if (key_sign == 0)
		{
			key_sign=1;
			// if (Key == 1)
			// {
			// 	stop_stop=0;//防止长按但是未按下时触发开机
			// }
		}
		
		printf(">> PA0:%d \n\r",Key);
    }
    
}